﻿using CommonUtils;
using GitIconOverlayHandlers;
using System;
using System.Windows;

/*
注册表地址 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers
*/

namespace GitIconOverlayor
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Loaded += MainWindow_Loaded;

            ButtonInstall.Click += ButtonInstall_Click;
            ButtonUninstall.Click += ButtonUninstall_Click;
            LabelUpdate.Click += LabelUpdate_Click;
            ButtonLogoff.Click += ButtonLogoff_Click;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                TextBoxLog.Log("Prioritized Status：" + GitIconOverlayHandler.PrioritizedStatus);
                TextBoxLog.Log("Application Version：" + ApplicationUtil.Version);
                TextBoxLog.Log("注意：不要在Tortoise中禁用系统缓存，否则会造成图标不显示！！！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private static string _asmPath = "RegAsm.exe";
        private static string _dllPath = typeof(GitIconOverlayHandler).Namespace + ".dll";

        private void ButtonInstall_Click(object sender, RoutedEventArgs e)
        {
            RegistryUtil.SetToConfig(GitIconOverlayHandler.Filed_WorkFolder, PathUtil.AppBase);
            TextBoxLog.Log("SetToRegeditConfig: " + GitIconOverlayHandler.Filed_WorkFolder + " = " + PathUtil.AppBase);
            TextBoxLog.Log(ProcessUtil.Run(_asmPath, _dllPath + " /codebase"));
            MessageBox.Show("安装完成，注销/重启后生效，请保留此文件夹");
        }

        private void ButtonUninstall_Click(object sender, RoutedEventArgs e)
        {
            RegistryUtil.RemoveFromConfig(GitIconOverlayHandler.Filed_WorkFolder);
            TextBoxLog.Log("RemoveFromRegeditConfig: " + GitIconOverlayHandler.Filed_WorkFolder);
            TextBoxLog.Log(ProcessUtil.Run(_asmPath, _dllPath + " /u"));
            MessageBox.Show("卸载完成，注销/重启后生效，注销/重启后可手动删除此文件夹");
        }

        private void LabelUpdate_Click(object sender, RoutedEventArgs e)
        => UrlUtil.Open("https://gitee.com/atalent/GitIconOverlayor/releases");

        private void ButtonLogoff_Click(object sender, RoutedEventArgs e)
        => CommandUtil.Logoff();
    }
}
